﻿using System;
using System.ComponentModel;
using Castle.DynamicProxy;
using NHibernate.Validator.Engine;
using TestBat.IModels.Base;
using TestBat.IModels.Factories;
using TestBat.Models.Interceptors.Domain;
using TestBat.ServiceLocator;

namespace TestBat.Models.Factories
{
    public class EntityFactory : IEntityFactory
    {
        private static readonly ProxyGenerator ProxyGenerator = new ProxyGenerator(); // TODO: use DI

        public T Create<T>()
        {
            return (T)Create(typeof(T));
        }

        public object Create(Type type)
        {
            return ProxyGenerator.CreateClassProxy(type, new []
		        {
                    typeof(INotifyPropertyChanged),
                    typeof(IMarker),
                    typeof(IDataErrorInfo),
                    //typeof(ILockable),
		        },
                new Castle.Core.Interceptor.IInterceptor[]
                {
                    new MarkerInterceptor(type.FullName),
                    new NotifyPropertyChangedInterceptor(), 
                    new DataErrorInfoInterceptor(DefaultServiceLocator.Get<ValidatorEngine>()),
                    //new LockableInterceptor(),
                }
            );
        }
    }
}
